package com.action.system;

import java.lang.reflect.Method;
import java.util.List;

import com.base.BaseAction;
import com.bean.RoleInfo;
import com.bean.RulerInfo;
import com.bean.RulerMethod;
import com.bean.RulerRole;
import com.service.RoleService;
import com.service.RulerService;
import com.util.GUtil;

public class RoleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	RoleService roleService;
	List<RoleInfo> list;
	RoleInfo roleInfo;
	@Deprecated
	String rightString;
	List<RulerInfo> rulerList;
	List<RulerMethod> methodsList;
	List<RulerRole> rulerRoleList;
	
	@Override
	public String handle() throws Exception {
		
		if(method==null  ){
			return query();
		}
		return GUtil.NONE;
	}

	public String add() throws Exception{
		this.setHeaderTitle("添加角色");
		//beforeAdd
		if(roleInfo==null){
			this.beforeAddOrUpdate();
			roleInfo = null;
			return GUtil.EDIT;
		}
		
		//add
		if( roleInfo.getRoleName()==null || roleInfo.getRoleName().trim().isEmpty()){
			this.setMsg("角色名称不能为空");
			return GUtil.EDIT;
		}
		
		list = roleService.findByProperty("roleName", roleInfo.getRoleName());
		if(list!=null && list.size() > 0 ){
			this.setMsg("此角色已存在");
			this.beforeAddOrUpdate();
			return GUtil.EDIT;
		}
		
		for(RulerInfo ri1 : rulerList){
			if(ri1==null)
				continue;
			System.out.print( " rulerId="+ri1.getRulerid() +" methods=");
			List<RulerMethod> list2 = ri1.getMethods();
			if(list2!=null){
				for(RulerMethod rm :list2){
					if(rm==null)
						continue;
					System.out.print(" "+ rm.getMethod() + "," ); 
				}
			}
			System.out.println();
		}
		String result = "";
		result = roleService.add(roleInfo, rightString, rulerList);
		if(result.equals(GUtil.SUCCESS)){
			msg = "操作成功";
			roleInfo=null;
			return query();
		}else {
			msg = "操作失败";
			return GUtil.EDIT;
		}
		//////
	}
	
	public boolean beforeAddOrUpdate()throws Exception{
		//根据user.roleId获取此管理员可以控制的菜单，然后在根据id获取次update角色的菜单
		Long t1 = System.currentTimeMillis();
		rulerList = rulerService.query(null);
		methodsList = rulerService.queryMethds( null, true);//获取 RulerMethod列表(去除重复)
		if(id!=null){
			rulerRoleList = rulerService.queryMyRuler( id );//根据角色信息获取角色已有的权限列表
		}
		Long t2 = System.currentTimeMillis();

		String str = "\n";
		int count = 0;
		//设置各个菜单的序号number,前台jsp页面需要使用
		for(RulerInfo ri : rulerList){//遍历一级菜单
			ri.setNumber(count++);
			str += "   "+ ri.getRulerid()+"-" + ri.getRulerName() + "\n";
			for(RulerInfo subruler : ri.getSubRulerList()){//遍历子菜单
				subruler.setNumber(count++);//1. set number 
				//合并
				if(id!=null && rulerRoleList!=null){//如果是update,合并设置checked
					for(RulerRole rr : rulerRoleList){//变量rulerrole
						if( (rr.getRulerId() - subruler.getRulerid())==0 ){//rulerole.id==ruler.id
							for(RulerMethod m : subruler.getMethods()){//变量subruler.methods
								String rulerwords = rr.getRulerWord();
								if(rulerwords!=null){
									String rulerword[] = rulerwords.split(";");
									for(String rw : rulerword){//方法匹配查找
										if(rw.trim().equals(m.getMethod())){
											m.setChecked(true);
											break;
										}
									}
								}
							}//end of 变量subruler.methods
							subruler.setChecked(true);
							ri.setChecked(true);
						}//end of rulerole.id==ruler.id
					}//遍历一级菜单
				}//变量rulerrole
				
				//显示二级菜单
				str += ("   "+"  |---"+subruler.getRulerid()+"-"+subruler.getRulerName()+"-"+subruler.getSortorder());
				//显示methods
				for(RulerMethod rm :subruler.getMethods()){
					str += ( "  "+rm.getMethod()+"="+rm.getName() + " checked="+ rm.getChecked());
				}
				str += "\n";
			}//遍历子菜单
		}//遍历一级菜单
		
		Long t3 = System.currentTimeMillis();
		this.setLogmsg("count="+count+",查询耗时:"+(t2-t1)+"ms,计算耗时:"+(t3-t2)+"ms,data="+str);
		
		return true;
		
	}

	public String update() throws Exception {
		 //beforeUpdate
		if(roleInfo==null){
			roleInfo = roleService.get(id);//获取角色信息
			session.put("roleInfo", roleInfo);
			if(roleInfo==null){
				this.setMsg("信息不存在");
				return GUtil.ERROR;
			}if(roleInfo.getFlag()==0){
			//	this.setMsg("系统默认角色不可更改");
			//	return GUtil.ERROR;//0:系统默认角色不可更改
			}
			this.beforeAddOrUpdate();
			this.setId(roleInfo.getRoleId());
			return GUtil.EDIT;
		}
		
		//Update
		RoleInfo ri = (RoleInfo) session.get("roleInfo");
		if(ri==null){
			this.setMsg("请求超时");
			return GUtil.ERROR;
		}
		roleInfo.setRoleId(ri.getRoleId());
		roleInfo.setFlag(ri.getFlag());
		if(roleInfo.getStatus()==null || (roleInfo.getStatus()!=1 || roleInfo.getStatus()!=0)){
			roleInfo.setStatus(ri.getStatus());
		}
		if(roleInfo.getFlag()==0){
			//系统默认角色不可更改
			roleInfo.setRoleName(ri.getRoleName());
		}else{
			if(roleInfo.getRoleName()==null || roleInfo.getRoleName().trim().isEmpty()){
				this.setMsg("角色名称不能为空");
				return GUtil.EDIT;
			}
		}
		for(RulerInfo ri1 : rulerList){
			if(ri1==null)
				continue;
			System.out.print( " rulerId="+ri1.getRulerid() +" methods=");
			List<RulerMethod> list2 = ri1.getMethods();
			if(list2!=null){
				for(RulerMethod rm :list2){
					if(rm==null)
						continue;
					System.out.print(" "+ rm.getMethod() + "," ); 
				}
			}
			System.out.println();
		}
		String result = "";
		result = roleService.update(roleInfo, rightString, rulerList);
		if(result.equals(GUtil.SUCCESS)){
			msg = "操作成功";
			roleInfo=null;
			return query();
		}else {
			msg = "操作失败";
			return GUtil.EDIT;
		}
	}

	public String query() throws Exception {
		list=roleService.query(roleInfo);
		return GUtil.SUCCESS;
	}
	

	public String delete() throws Exception {
		
		if(id==null || id<0){
			msg = "操作失败,错误请求";
			return GUtil.ERROR;
		}
		
		RoleInfo ri = roleService.get( id );
		if(ri==null || ri.getStatus()!=1){
			msg = "操作失败,错误请求";
			return GUtil.ERROR;
		}
		if(ri.getFlag()<1){
			msg = "系统默认角色不可以删除";
			return GUtil.ERROR;
		}
		
		String result=roleService.delete(id, 0);
		if(result.equals(GUtil.SUCCESS)){
			msg = "操作成功";
			return query();
		}
		else {
			msg = "操作失败";
			return GUtil.SUCCESS;
		}
	}


	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<RoleInfo> getList() {
		return list;
	}

	public void setList(List<RoleInfo> list) {
		this.list = list;
	}

	public RoleInfo getRoleInfo() {
		return roleInfo;
	}

	public void setRoleInfo(RoleInfo roleInfo) {
		this.roleInfo = roleInfo;
	}

	public String getRightString() {
		return rightString;
	}

	public void setRightString(String rightString) {
		this.rightString = rightString;
	}

	@Override
	public String view() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * @return the rulerList
	 */
	public List<RulerInfo> getRulerList() {
		return rulerList;
	}

	/**
	 * @param rulerList the rulerList to set
	 */
	public void setRulerList(List<RulerInfo> rulerList) {
		this.rulerList = rulerList;
	}

	/**
	 * @return the methodsList
	 */
	public List<RulerMethod> getMethodsList() {
		return methodsList;
	}

	/**
	 * @param methodsList the methodsList to set
	 */
	public void setMethodsList(List<RulerMethod> methodsList) {
		this.methodsList = methodsList;
	}


	
	
}
